package org.example.yunpicturebackend.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ConfigurationProperties(prefix = "cos.client")
@Data
public class CosClientConfig {

 private String host;
 private String secretId;
 private String secretKey;
 private String region;
 private String bucket;

 @Bean
 public COSClient cosClient() {
  // 打印配置值，确认已正确注入
  log.info("Creating COSClient with secretId: {}", this.secretId);
  log.info("Creating COSClient with secretKey: {}", this.secretKey);
  log.info("Creating COSClient with region: {}", this.region);

  // 使用类成员变量，而不是环境变量
  COSCredentials cred = new BasicCOSCredentials(this.secretId, this.secretKey);

  // 设置 bucket 的地域
  ClientConfig clientConfig = new ClientConfig(new Region(this.region));

  // 使用 https 协议
  clientConfig.setHttpProtocol(HttpProtocol.https);

  // 生成 cos 客户端
  return new COSClient(cred, clientConfig);
 }
}